package com.q.tqb.view.dialog;


import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Point;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonElement;
import com.q.tqb.R;
import com.q.tqb.activity.login.AgrementActivity;
import com.q.tqb.activity.login.LoginActivity;
import com.q.tqb.activity.send.SendSpeakingActivity;
import com.q.tqb.base.AppConst;
import com.q.tqb.base.BaseApplication;
import com.q.tqb.util.DensityUtil;
import com.q.tqb.util.LogUtil;
import com.q.tqb.util.http.IResponse;
import com.q.tqb.util.http.OkHttpUtil;
import com.tencent.mm.opensdk.modelmsg.SendAuth;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArraySet;


public class PayDialog extends Dialog implements View.OnClickListener {

    private CheckBox cbBalance;
    private CheckBox cbWx;

    private Activity mActivity;
    private int payType = 2;
    private double allMoney;
    private String myMoney;

    public PayDialog(Activity activity, double allMoney, String myMoney) {
        super(activity, R.style.myDialog);
        this.mActivity = activity;
        this.allMoney = allMoney;
        this.myMoney = myMoney;
        initLoginDialog();
    }

    private void initLoginDialog() {

        View v = View.inflate(mActivity, R.layout.dialog_pay, null);
        TextView tvMoney = v.findViewById(R.id.tv_money);
        TextView tvBalance = v.findViewById(R.id.tv_balance);
        TextView tvPay = v.findViewById(R.id.tv_pay);
        LinearLayout llBalance = v.findViewById(R.id.ll_balance);
        LinearLayout llWx = v.findViewById(R.id.ll_wx);
        tvMoney.setText(allMoney + "");
        tvBalance.setText("¥" + myMoney + "");

        cbBalance = v.findViewById(R.id.cb_balance);
        cbWx = v.findViewById(R.id.cb_wx);
        cbWx.setChecked(true);

        llBalance.setOnClickListener(this);
        llWx.setOnClickListener(this);
        cbWx.setOnClickListener(this);
        cbBalance.setOnClickListener(this);
        cbBalance.setOnClickListener(this);
        tvPay.setOnClickListener(this);


        Point size = new Point();
        mActivity.getWindowManager().getDefaultDisplay().getSize(size);

        //250
        int height = DensityUtil.dip2px(mActivity, 290);
        setContentView(v);
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.CENTER);
        lp.height = height;
        lp.width = (int) (size.x * 0.8);
        dialogWindow.setAttributes(lp);


    }
    public void dimssCount(){
        findViewById(R.id.ll_balance).setVisibility(View.GONE);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cb_balance:
            case R.id.ll_balance:
                payType = 1;
                cbBalance.setChecked(true);
                cbWx.setChecked(false);
                break;
            case R.id.cb_wx:
            case R.id.ll_wx:
                payType = 2;
                cbWx.setChecked(true);
                cbBalance.setChecked(false);
                break;
            case R.id.tv_pay:
                // payMoney();
                dismiss();
                if (payType == 1) {
                    double myDouMoney = 0;
                    try {
                        myDouMoney = Double.valueOf(myMoney);
                    } catch (Exception e) {

                    }
                    if (myDouMoney >= allMoney) {
                        mCommonListener.onClick(payType);
                    } else {
                        Toast.makeText(mActivity, "余额不足,请使用微信支付", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    mCommonListener.onClick(payType);
                }
                break;
            default:
                break;
        }

    }


    public interface CommonListener {
        void onClick(int type);
    }

    private CommonListener mCommonListener;

    public void setOnSettingListener(CommonListener listener) {
        this.mCommonListener = listener;
    }


}
